home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / QuickDraw / ColorizePict / Source / PictInfoTest.c < prev   
Encoding:
C/C++ Source or Header  |  1996-09-17  |  9.7 KB  |  442 lines  |  [TEXT/CWIE]

  1. // buffered window shell App.  This file creates a window and attaches a GWorld to it, created
  2. // from information contained in the source picture.  You can open a picture and display it
  3. // in the window.  This is a shell for testing developer questions about offscreens.
  4. //
  5. // There are two good references for the offscreen stuff.  Inside Macintosh: Imaging with Quickdraw
  6. // and Scott Knaster & Keith Rollin'sbook: Macintosh Programming Secrets.  Both of these are available from 
  7. // Addison Wesley.
  8. //
  9. // Nick Thompson - June 6th 1994
  10.  
  11. /* Constant Declarations */
  12. #include <Fonts.h>
  13. #include <Processes.h>
  14. #include <Dialogs.h>
  15. #include <Resources.h>
  16. #include <menus.h>
  17. #include <PictUtil.h>
  18. #include <QDOffScreen.h>
  19. #include <DiskInit.h>
  20. #include <StandardFile.h>
  21. #include <Devices.h>
  22.  
  23.  
  24. #define    WWIDTH        470
  25. #define    WHEIGHT        330
  26.  
  27. #define WLEFT        (((screenBits.bounds.right - screenBits.bounds.left) - WWIDTH) / 2)
  28. #define WTOP        (((screenBits.bounds.bottom - screenBits.bounds.top) - WHEIGHT) / 2)
  29.  
  30. #define HiWrd(aLong)    (((aLong) >> 16) & 0xFFFF)
  31. #define LoWrd(aLong)    ((aLong) & 0xFFFF)
  32.  
  33.  
  34.  
  35. enum {
  36.     mApple = 128,
  37.     mFile,
  38.     mEffects
  39. } ;
  40.  
  41. enum {
  42.     iAbout = 1
  43. } ;
  44. enum {
  45.     iOpen = 1,
  46.     iClose,
  47.     iUnused1,
  48.     iQuit = 4
  49. } ;
  50.  
  51. enum {
  52.     iUsePictPalette = 1
  53. } ;
  54.  
  55. static Boolean gQuitFlag = false ;
  56. static Point gStaggerPos = {50,50} ;
  57. static Boolean gUsePictPalette = true ;
  58.  
  59. // function prototypes
  60.  
  61. void InitToolbox( void ) ;
  62. void MainEventLoop( void ) ;
  63. void HandleKeyPress(EventRecord *event) ;
  64. void HandleOSEvent(EventRecord *event) ;
  65. void HandleMenuCommand(long menuResult) ;
  66. PicHandle DoReadPICT( short theRef, OSErr *theErr ) ;
  67. OSErr DoCreateWindow( PicHandle thePicture ) ;
  68. void AdjustMenus( void ) ;
  69. void ColorizeImage( WindowPtr imageToColorize ) ;
  70.  
  71.  
  72. const RGBColor    kRGBBlack = {0, 0, 0};
  73. const RGBColor    kRGBWhite = {0xFFFF, 0xFFFF, 0xFFFF};
  74.  
  75.  
  76. void main(void)
  77. {
  78.     InitToolbox() ;
  79.     
  80.     MainEventLoop();
  81. }
  82.  
  83.  
  84.  
  85. void InitToolbox()
  86. {
  87.     /*OSErr         retCode;*/
  88.     /*long         gestResponse;*/
  89.     Handle        menuBar = nil;
  90.     /*EventRecord event;*/
  91.     /*short        count;*/
  92.  
  93.  
  94.     InitGraf((Ptr) &qd.thePort);
  95.     InitFonts();
  96.     InitWindows();
  97.     InitMenus();
  98.     TEInit();
  99.     InitDialogs((long)nil);
  100.     InitCursor();
  101.  
  102.     // initialize application globals
  103.     
  104.     gQuitFlag = false;
  105.     
  106.     
  107.     menuBar = GetNewMBar(128);                // Read menus into menu bar, MBAR res id is 128
  108.     
  109.     if ( menuBar == nil )
  110.          ExitToShell();                        // if we dont have it then quit - your app 
  111.                                              // needs a dialog here
  112.  
  113.     SetMenuBar(menuBar);                    // Install menus
  114.     DisposeHandle(menuBar);
  115.     
  116.     AppendResMenu(GetMenuHandle(mApple), 'DRVR');    // Add DA names to Apple menu, ID 128
  117.  
  118.     DrawMenuBar();
  119. }
  120.  
  121.  
  122. void MainEventLoop()
  123. {
  124.     EventRecord     event;
  125.     WindowPtr       window;
  126.     short           thePart;
  127.     Rect            screenRect;
  128.     Point            aPoint = {100, 100};
  129.     GWorldPtr        theNewWorld ;
  130.     PixMapHandle    offPixMap ;
  131.     GrafPtr            oldPort ;
  132.  
  133.     while( !gQuitFlag )
  134.     {
  135.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  136.         {
  137.             AdjustMenus() ;
  138.  
  139.             switch (event.what) {
  140.                 case mouseDown:
  141.                 
  142.                     thePart = FindWindow( event.where, &window );
  143.                     
  144.                     switch( thePart ) {
  145.                         case inMenuBar: 
  146.                             HandleMenuCommand(MenuSelect(event.where));
  147.                             break;
  148.                         
  149.                         case inDrag:
  150.                     
  151.                             screenRect = (**GetGrayRgn()).rgnBBox;
  152.                             DragWindow( window, event.where, &screenRect );
  153.                             break ;
  154.                     
  155.                         case inContent:
  156.                     
  157.                             if (window != FrontWindow())
  158.                                 SelectWindow( window );
  159.                             break ;
  160.                     
  161.                         case inGoAway:
  162.                             if (TrackGoAway( window, event.where )) {
  163.                                 DisposeWindow ( window );
  164.                             }
  165.                             break ;
  166.                             
  167.                         default:
  168.                             break ;
  169.                     }
  170.                     break ;
  171.                             
  172.                         
  173.                 case updateEvt:
  174.                 
  175.                     window = (WindowPtr)event.message;
  176.                     GetPort(&oldPort ) ;    
  177.                     SetPort( window );
  178.                     
  179.                     BeginUpdate( window );
  180.                     
  181.                     // get the GWorld from the window refcon
  182.                     theNewWorld = (GWorldPtr)GetWRefCon ( window );
  183.                     offPixMap = GetGWorldPixMap( theNewWorld ) ;
  184.                     (void) LockPixels( offPixMap ) ;
  185.                     CopyBits( &((GrafPtr)theNewWorld)->portBits,
  186.                               &window->portBits,
  187.                               &window->portRect,
  188.                               &window->portRect,
  189.                               srcCopy,
  190.                               nil ) ;
  191.                     (void) UnlockPixels( offPixMap ) ;
  192.  
  193.                     EndUpdate( window );
  194.                     SetPort( oldPort ) ;
  195.                     break ;
  196.                     
  197.                 case keyDown:
  198.                 case autoKey:
  199.                     HandleKeyPress(&event);
  200.                     break;
  201.                     
  202.                 case diskEvt:
  203.                     if ( HiWrd(event.message) != noErr ) 
  204.                         (void) DIBadMount(aPoint, event.message);
  205.                     break;
  206.                     
  207.                 case osEvt:
  208.                 case activateEvt:
  209.                     break;
  210.  
  211.  
  212.             }
  213.         }
  214.     }
  215. }
  216.  
  217.  
  218. void HandleKeyPress(EventRecord *event)
  219. {
  220.     char    key;
  221.  
  222.     key = event->message & charCodeMask;
  223.     
  224.     // just check to see if we want to quit...
  225.     
  226.     if ( event->modifiers & cmdKey ) {        /* Command key down? */
  227.         HandleMenuCommand(MenuKey(key));
  228.     } 
  229. }
  230.  
  231.  
  232. void HandleMenuCommand(long menuResult)
  233. {
  234.     short        menuID;
  235.     short        menuItem;
  236.     Str255        daName;
  237.     DialogPtr    theDialog ; 
  238.     short        itemHit ;
  239.     SFTypeList    myTypes = { 'PICT' } ;
  240.     /*FSSpec        theFSSpec ;*/
  241.     PicHandle    thePicture ;
  242.     OSErr        err ;
  243.     short        theRef ;
  244.     
  245.     StandardFileReply    theSFReply ;
  246.  
  247.     menuID = HiWrd(menuResult);
  248.     menuItem = LoWrd(menuResult);
  249.     switch ( menuID ) {
  250.         case mApple:
  251.             switch ( menuItem ) {
  252.                 ModalFilterUPP         theProc ;
  253.  
  254.                 case iAbout:
  255.                 
  256.                     theDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
  257.                     
  258.                     // these two lil' snappers are system 7 only
  259.                     // so if you use them, check before!!
  260.                     GetStdFilterProc( &theProc ) ;
  261.                     SetDialogDefaultItem(theDialog, ok) ;
  262.                     
  263.                     
  264.                     do {
  265.                         ModalDialog ( theProc, &itemHit );
  266.                     } while( itemHit != ok ) ;
  267.                     DisposeDialog ( theDialog );
  268.                     break;
  269.                     
  270.                 default:
  271.                     GetMenuItemText(GetMenuHandle(mApple), menuItem, daName);
  272.                     (void) OpenDeskAcc((ConstStr255Param)daName);
  273.                     break;
  274.             }
  275.             break;
  276.         case mFile:
  277.             switch ( menuItem ) {
  278.                 case iOpen:
  279.                     // Get the file name to open
  280.                     StandardGetFile( nil, 1, myTypes, &theSFReply ) ;
  281.                     
  282.                     // did the user cancel?
  283.                     if(!theSFReply.sfGood)
  284.                         break ;
  285.                     
  286.                     // open the file
  287.                     err = FSpOpenDF( &theSFReply.sfFile, fsRdPerm, &theRef ) ;
  288.                     
  289.                     if( err != noErr )
  290.                         break ;     // should handle this properly
  291.                         
  292.                     thePicture = DoReadPICT( theRef, &err ) ;
  293.                     
  294.                     if( err != noErr )
  295.                         break ;     // should handle this properly
  296.                 
  297.                     // display the contents
  298.                     err = DoCreateWindow( thePicture ) ;
  299.                     
  300.                     break ;
  301.                     
  302.                 case iClose:
  303.                     DisposeWindow ( FrontWindow() );
  304.                     break ;
  305.                 case iQuit:
  306.                     gQuitFlag = true;
  307.                     break;
  308.             }
  309.             break;
  310.             
  311.             
  312.         case 131:
  313.             if( menuItem == 1 )
  314.                 if( FrontWindow() != nil )
  315.                     ColorizeImage( FrontWindow() ) ;
  316.             break; 
  317.  
  318.     }
  319.     HiliteMenu(0);        // Unhighlight whatever MenuSelect or MenuKey hilited
  320. }
  321.  
  322. void AdjustMenus( void ) 
  323. {
  324.     WindowPtr    theWindow ;
  325.     theWindow = FrontWindow() ;
  326.     if( theWindow != nil ) {
  327.         EnableItem ( GetMenuHandle ( mFile ), iClose );
  328.     }
  329.     else {
  330.         DisableItem ( GetMenuHandle ( mFile ), iClose );
  331.     }
  332. }
  333.  
  334. PicHandle DoReadPICT( short theRef, OSErr *theErr ) 
  335. {
  336.     long        theFileSize ;
  337.     PicHandle    thePicture ;
  338.     
  339.     // pict files have a 512 byte header at the front - we dont care about this
  340.     // we can find the size of the pict by subtracting 512 bytes from the length
  341.     // of the file.  We then want to resize the handle to that and read the data
  342.     // into the resized handle.
  343.     
  344.     if(( *theErr = GetEOF( theRef, &theFileSize )) != noErr ) {
  345.         FSClose( theRef ) ;
  346.         return nil ; 
  347.     }
  348.     
  349.     if(( *theErr = SetFPos( theRef, fsFromStart, 512)) != noErr ) {
  350.         FSClose( theRef ) ;
  351.         return nil ; 
  352.     }
  353.  
  354.     theFileSize -= 512 ;
  355.     
  356.     thePicture = (PicHandle)NewHandle( theFileSize ) ;
  357.     if( thePicture == nil ) {
  358.         FSClose( theRef ) ;
  359.         *theErr = MemError() ;
  360.         return nil ;         // what ever the mem manager error was
  361.     }
  362.     
  363.     HLock( (Handle)thePicture ) ;
  364.     *theErr = FSRead( theRef, &theFileSize, (Ptr)*thePicture ) ;
  365.     HUnlock(  (Handle)thePicture ) ;
  366.     
  367.     if( *theErr != noErr ) {
  368.         FSClose( theRef ) ;
  369.         return nil ; 
  370.     }
  371.  
  372.     return thePicture ;    
  373. }
  374.  
  375. OSErr DoCreateWindow( PicHandle thePicture )
  376. {
  377.  
  378.     Rect        theRect ;
  379.     OSErr        theErr ;
  380.     GWorldPtr    theNewWorld ;
  381.     CGrafPtr    savedPort ;
  382.     /*GWorldPtr    savedGWorld ;*/
  383.     WindowPtr    theWindow ;
  384.     GDHandle    oldDevice ;
  385.     
  386.     /*PictInfo        thePictInfo ;*/
  387.     PaletteHandle    thePictPalette = nil ;
  388.     CTabHandle        thePictCTab = nil ;
  389.     
  390.     // make an offscreen environment and image the pict into this
  391.     // Make a window the size of the pict
  392.     // store a reference to the GWorld in the Refcon of the window
  393.     // invalidate the window content area.
  394.     
  395.     theRect.top = (**thePicture).picFrame.top ;
  396.     theRect.left = (**thePicture).picFrame.left ;
  397.     theRect.bottom = (**thePicture).picFrame.bottom ;
  398.     theRect.right = (**thePicture).picFrame.right ;
  399.     
  400.     // create a gWorld 8 bits deep
  401.     
  402.     theErr = NewGWorld( &theNewWorld, 8, &theRect, thePictCTab, nil, 0L ) ;
  403.     
  404.     if( theErr != noErr ) 
  405.         return theErr ;
  406.     
  407.     // save the world
  408.     GetGWorld( &savedPort, &oldDevice ) ;
  409.     SetGWorld( theNewWorld, nil ) ;
  410.     
  411.     
  412.     RGBForeColor( &kRGBBlack ) ;        // ensure the fg and bg colors are 
  413.     RGBBackColor( &kRGBWhite ) ;        // as anticipated
  414.     
  415.     EraseRect( &theRect ) ;                // clear the area for the pict
  416.     PenMode( srcCopy ) ;                // ensure the t/f mode is as expected
  417.  
  418.     // render the image into the offscreen buffer
  419.     DrawPicture( thePicture, &theRect ) ;
  420.     
  421.     SetGWorld( savedPort, oldDevice ) ;
  422.     
  423.     // create the window
  424.     OffsetRect( &theRect, gStaggerPos.h, gStaggerPos.v) ;
  425.     gStaggerPos.h += 16 ;
  426.     gStaggerPos.v += 16 ;        // heh - should roll these around, but you wont 
  427.                                 // create more than a couple of windows, will you  :-)
  428.                                  
  429.     theWindow  = NewCWindow( nil, &theRect, "\pplayTime", true, 
  430.                                 documentProc, (WindowPtr)-1, true, (long)theNewWorld );    
  431.                     
  432.     // make sure it is visible
  433.     ShowWindow( theWindow ) ;
  434.     
  435.     SetGWorld( (CGrafPtr)theWindow, nil ) ;
  436.     
  437.     // invalidate the content region of the window - we don't do any drawing to it here.
  438.     InvalRect ( &theRect );
  439.     
  440.     SetGWorld( savedPort, oldDevice ) ;
  441.     return theErr;
  442. }